package org.aiesec.fortaleza.xmatch.model.validator;

import org.aiesec.fortaleza.xmatch.model.exceptions.XMatchDomainException;
import org.aiesec.fortaleza.xmatch.model.exceptions.XMatchMessageException;
import org.aiesec.fortaleza.xmatch.model.exceptions.XMatchDomainException.TypeMessage;
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;


@Name("validatorUtil")
@Scope(ScopeType.APPLICATION)
public class ValidatorUtil {

	@SuppressWarnings("unchecked")
	public void validate(Object object) throws XMatchDomainException {
		ClassValidator classValidator = new ClassValidator(object.getClass());
		InvalidValue[] invalidmessages = classValidator
				.getInvalidValues(object);
		XMatchDomainException xMatchDomainException = new XMatchDomainException();
		for (InvalidValue invalidValue : invalidmessages) {
			XMatchMessageException xMatchMessageException = new XMatchMessageException(
					invalidValue.getMessage(), TypeMessage.ERROR);
			xMatchDomainException.addMessage(xMatchMessageException);
		}
		if (invalidmessages.length > 0) {
			throw xMatchDomainException;
		}
	}
}
